<?php
class omeanalysts_mdl_ome_reship extends dbeav_model{

    //var $has_export_cnf = true;

    var $export_name = '仓库收到不扣减应收的快递退货表';

    public function table_name($real=false){
        $table_name = 'ome_delivery';
        if($real){
            return kernel::database()->prefix.$table_name;
        }else{
            return $table_name;
        }
    }

    public function count($filter=null){

        $where = $this->_filter($filter); 

        $sql = "SELECT count(*) as _count 
                  from sdb_ome_reship a
                  left join sdb_ome_reship_items b on a.reship_id=b.reship_id 
                  left join sdb_material_basic_material_ext ext on b.product_id=ext.bm_id
                  left join sdb_ome_branch bh on a.branch_id=bh.branch_id
                  left join sdb_ome_shop f on a.shop_id=f.shop_id 
                  left join sdb_ome_dly_corp dp on a.return_logi_name=dp.corp_id
                     where ".$where." ";

        $row = $this->db->select($sql);
        return intval($row[0]['_count']);
    }

    public function getlist($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null){
  

        if(!$filter['time_from'] && !$filter['time_to']){
            return false;
        }else{

            $where = $this->_filter($filter); 
          $sql = "SELECT a.t_begin,a.reship_bn,a.ax_returnid,a.return_type,a.returnreason,dp.`name` return_logi_name,bh.`name` branch_id,a.return_logi_no,
                  b.bn,a.is_check,a.send_status,ext.unit,ext.specifications spec,b.num number,a.memo  
                  from sdb_ome_reship a
                  left join sdb_ome_reship_items b on a.reship_id=b.reship_id 
                  left join sdb_material_basic_material_ext ext on b.product_id=ext.bm_id
                  left join sdb_ome_branch bh on a.branch_id=bh.branch_id
                  left join sdb_ome_shop f on a.shop_id=f.shop_id 
                  left join sdb_ome_dly_corp dp on a.return_logi_name=dp.corp_id
                           where ".$where." ";
            $rows = $this->db->selectLimit($sql,$limit,$offset);
          

            return $rows;
        }
        
    }
    
    public function searchOptions(){
        $parentOptions = parent::searchOptions();
        $childOptions = array(
            'reship_bn'=>app::get('base')->_('订单号'),
        );
        return $Options = array_merge($parentOptions,$childOptions);
    }

    public function _filter($filter,$tableAlias=null,$baseWhere=null){


        $where = array(1);

       $where[]=" a.return_type='nosrc' ";
       if(empty($filter['time_from']))
        {
          $where[]=' 1=2 ';
        }
        if(isset($filter['time_from']) && $filter['time_from']){
            $where[] = ' a.t_begin >='.strtotime($filter['time_from']);
        }
        if(isset($filter['time_to']) && $filter['time_to']){
            $where[] = ' a.t_begin <'.(strtotime($filter['time_to'])+86399);
        }
        if(isset($filter['branch'])   && $filter['branch']){
            $where[] = ' bh.branch_id  =\''.addslashes($filter['branch']).'\'';
        }
        if(!$filter['branch'] && $filter['branch']){
            $where[] = ' bh.branch_id IN (\''.implode('\',\'', $filter['branch']).'\')';
        }
        unset($filter['branch']);

        if(isset($filter['p_bn']) && $filter['p_bn']){
            $where[] = ' b.bn =\''.addslashes($filter['p_bn']).'\'';
        }
        if(isset($filter['reship_bn']) && $filter['reship_bn']){
            $where[] = ' a.reship_bn =\''.addslashes($filter['reship_bn']).'\'';
        }
          
        return implode($where,' AND ');
    }

    public function get_schema(){
        $schema = array (
            'columns' => array (
                't_begin' =>
                array (
                  'type' => 'time',
                  'label' => '单据仓库日期',
                  'comment' => '单据仓库日期',
                  'editable' => false,
                  'in_list' => true,
                ),
                'reship_bn' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => '退换货单号',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ),
                'ax_returnid' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => 'AX日记账编号',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '100',
                ),
                'return_type' =>
                array (
                  'type' =>
                  array (
                    'return' => '退货',
                    'change' => '换货',
                    'refuse' => '拒收退货',
                    'nosrc' => '无来源退货',
                  ),
                  'default' => 'return',
                  'required' => true,
                  'comment' => '换货类型',
                  'editable' => false,
                  'label' => '换货类型',
                ),
                'returnreason' =>
                array(
                  'type' =>'varchar(320)',
                  'comment' => '退货退款原因',
                  'editable' => false,
                  'label' => '退货退款原因',
                  'in_list' => true,
                  'default_in_list' => true,
                ),
                'return_logi_name' =>
                array (
                  'type' => 'varchar(100)',
                  'label' => '退回物流公司',
                  'comment' => '退回物流公司',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'width' =>75,
                  'in_list' => true,
                  'default_in_list' => true,
                ),
                'branch_id' =>
                array(
                    'type' => 'varchar(32)',
                    'required' => true,
                    'label' => '退回仓库（AX）',
                    'is_title' => true,
                    'width' => 125,
                    'searchtype' => 'nequal',
                    'editable' => false,
                    'filtertype' => 'normal',
                    'filterdefault' => true,
                    'in_list' => true,
                    'default_in_list' => false,
                ),
                'return_logi_no' =>
                array (
                  'type' => 'varchar(50)',
                  'label' => '退回物流单号',
                  'comment' => '退回物流单号',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'width' =>130,
                  'in_list' => true,
                  'default_in_list' => true,
                ),
                'bn' =>
                array (
                  'type' => 'varchar(30)',
                  'label' => '物料编码',
                  'editable' => false,
                  'is_title' => true,
                ),
                'is_check' =>
                array(
                  'type' => array(
                    0 => '未审核',
                    1 => '审核成功',
                    2 => '审核失败',
                    3 => '收货成功',
                    4 => '拒绝收货',
                    5 => '拒绝',
                    6 => '补差价',
                    7 => '完成',
                    8 => '质检通过',
                    9 => '拒绝质检',
                    10 => '质检异常',
                    11 => '待确认',
                    12=>'收货异常',
                    13 => '质检中',
                    14=>'过数关闭'
                  ),
                  'default'=>'0',
                  'comment' => '当前状态',
                  'editable' => false,
                  'label' => '当前状态',
                  'width' =>65,
                  'in_list' => true,
                  'default_in_list' => true,
                ),
                'send_status' =>
                array(
                  'type' => array(
                    0 => '未同步',
                    1 => '已同步',
                  ),
                  'default'=>'0',
                  'comment' => '同步erp状态',
                  'editable' => false,
                  'label' => '同步erp状态',
                  'in_list' => true,
                  'default_in_list' => true,
                ),
                'unit' =>
                array (
                  'type' => 'varchar(200)',
                  'required' => true,
                  'label' => '单位',
                  'is_title' => true,
                  'default_in_list' => true,
                  'width' => 70,
                  'searchtype' => 'has',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                ),
                'spec'=>
                array(
                    'type'=>'varchar(32)',
                    'label'=>'规格',
                    'in_list' => true,
                    'default_in_list' => true,
                ),
                'number' =>
                array (
                  'type' => 'number',
                  'label' => '数量',
                  'required' => true,
                  'default' => 0,
                  'editable' => false,
                ),
                'memo' =>
                array (
                  'type' => 'longtext',
                  'label' => '备注',
                  'comment' => '备注',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'in_list' => true,
                ),
            ),
            'idColumn' => 'name',
            'in_list' => array (
                0 => 't_begin',
                1 => 'reship_bn',
                2 => 'ax_returnid',
                3 => 'return_type',
                4 => 'returnreason',
                5 => 'return_logi_name',
                6 => 'branch_id',
                7 => 'return_logi_no',
                8 => 'bn',
                9 => 'is_check',
                10 => 'send_status',
                11 => 'unit',
                12 => 'spec',
                13 => 'number',
                14 => 'memo',                
            ),
            'default_in_list' => array (
                0 => 't_begin',
                1 => 'reship_bn',
                2 => 'ax_returnid',
                3 => 'return_type',
                4 => 'returnreason',
                5 => 'return_logi_name',
                6 => 'branch_id',
                7 => 'return_logi_no',
                8 => 'bn',
                9 => 'is_check',
                10 => 'send_status',
                11 => 'unit',
                12 => 'spec',
                13 => 'number',
                14 => 'memo', 
            ),
        );
        return $schema;
    }

    /**
     * 获得日志类型(non-PHPdoc)
     * @see dbeav_model::getLogType()
     */
    public function getLogType($logParams) {
        $type = $logParams['type'];
        $logType = 'none';
        if ($type == 'export') {
            $logType = $this->exportLogType($logParams);
        }
        elseif ($type == 'import') {
            $logType = $this->importLogType($logParams);
        }
        return $logType;
    }
    /**
     * 导出日志类型
     * @param Array $logParams 日志参数
     */
    public function exportLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_export';
        return $type;
    }
    /**
     * 导入操作日志类型
     * @param Array $logParams 日志参数
     */
    public function importLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_import';
        return $type;
    }

    function io_title( $filter, $ioType='csv' ){


        switch( $filter ){
            case 'delivery':
                $this->oSchema['csv'][$filter] = array(
                    '*:单据仓库日期' => 't_begin',
                    '*:退换货单号' => 'reship_bn',
                    '*:AX日记账编号' => 'ax_returnid',
                    '*:换货类型' => 'return_type',
                    '*:退回原因' => 'returnreason',
                    '*:退回物流公司' => 'return_logi_name',
                    '*:退回仓库（AX）' => 'branch_id',
                    '*:退回物流单号' => 'return_logi_no',
                    '*:物料编码' => 'bn',
                    '*:当前状态' => 'is_check',
                    '*:同步erp状态' => 'send_status',
                    '*:单位' => 'unit',
                    '*:规格' => 'spec',
                    '*:数量' => 'number',
                    '*:备注' => 'memo',
                );
                break;
        }
        $this->ioTitle[$ioType][$filter] = array_keys( $this->oSchema[$ioType][$filter] );
        return $this->ioTitle[$ioType][$filter];
    }

    //根据查询条件获取导出数据
    public function getExportDataByCustom($fields, $filter, $has_detail, $curr_sheet, $start, $end){
          //根据选择的字段定义导出的第一行标题
          if($curr_sheet == 1){

              $title = array();
              foreach($this->io_title('delivery') as $k => $v ){
                  $title[] = mb_convert_encoding($v, 'GBK', 'UTF-8');
              }

              $data['content']['main'][0] = '"'.implode('","',$title).'"';
          }
          $is_check=array(
                  0 => '未审核',
                  1 => '审核成功',
                  2 => '审核失败',
                  3 => '收货成功',
                  4 => '拒绝收货',
                  5 => '拒绝',
                  6 => '补差价',
                  7 => '完成',
                  8 => '质检通过',
                  9 => '拒绝质检',
                  10 => '质检异常',
                  11 => '待确认',
                  12=>'收货异常',
                  13 => '质检中',
                  14=>'过数关闭'
                );
          $return_type=array (
              'return' => '退货',
              'change' => '换货',
              'refuse' => '拒收退货',
              'nosrc' => '无来源退货',
              'onlyreturn' => '仅退货',
            );

          $where = $this->_filter($filter);
          $sql = "SELECT a.t_begin,a.reship_bn,a.ax_returnid,a.return_type,a.returnreason,dp.`name` return_logi_name,bh.`name` branch_id,a.return_logi_no,
                  b.bn,a.is_check,a.send_status,ext.unit,ext.specifications spec,b.num number,a.memo  
                  from sdb_ome_reship a
                  left join sdb_ome_reship_items b on a.reship_id=b.reship_id 
                  left join sdb_material_basic_material_ext ext on b.product_id=ext.bm_id
                  left join sdb_ome_branch bh on a.branch_id=bh.branch_id
                  left join sdb_ome_shop f on a.shop_id=f.shop_id 
                  left join sdb_ome_dly_corp dp on a.return_logi_name=dp.corp_id
                  where ".$where." ";

            $omedelivery = $this->db->selectLimit($sql,$end,$start);
            foreach ($omedelivery as $key => $value) 
            {
                $omedelivery[$key]['t_begin'] =date('Y-m-d h:i:s',$value['t_begin']);
                $omedelivery[$key]['return_type'] =$return_type[$value['return_type']];
                $omedelivery[$key]['is_check'] =$is_check[$value['is_check']];
                if($value['send_status']=='0')
                {
                  $omedelivery[$key]['send_status'] ='未同步';
                }else{
                  $omedelivery[$key]['send_status'] ='已同步';
                }
             
            }

            foreach ($omedelivery as $k => $aFilter) {
                $exptmp_data = array();
                foreach ($aFilter as $col => $val) {
                    $aFilter[$col] = mb_convert_encoding($aFilter[$col], 'GBK', 'UTF-8');
                    $exptmp_data[] = $aFilter[$col];
                }
                $data['content']['main'][] = implode(',', $exptmp_data);
            }
            return $data;
        
    }
}
